package com.spike.controller;

import com.spike.common.R;
import com.spike.model.Book;
import com.spike.model.User;
import com.spike.service.BooksService;
import com.spike.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@AllArgsConstructor
@RequestMapping("/books")
public class BooksController {
    private BooksService booksService;
    @PostMapping("add")
    public R addBook(@RequestBody Book book) {
        Integer bookId = booksService.addBook(book);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("bookId", bookId);
        return new R(200, "添加成功", map);
    }
    @PutMapping("{book_id}")
    public R updateBook(@RequestBody Book book, @PathVariable("book_id") Integer bookId) {
        Boolean bool = booksService.updateBook(book,bookId);
        if (bool) {
            return new R(200, "更新成功", null);
        }else
            return new R(409, "版本冲突，请刷新数据", null);
    }
    @PostMapping("{book_id}/delete")
    public R deleteUser(@PathVariable("book_id") Integer bookId,@RequestBody User user) {
        Boolean delete = booksService.delete(bookId,user.getVersion());
        if (delete) {
            return new R(200,"删除成功",null);
        }else
            return new R(409,"版本冲突，请刷新数据",null);
    }
}
